package com.rednose.videoplayer.dialogs;

import static com.rednose.videoplayer.activities.VideoFilesActivity.MY_PREF;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.rednose.videoplayer.model.MediaFiles;
import com.rednose.videoplayer.R;
import com.rednose.videoplayer.adapters.VideoFilesAdapter;

import java.util.ArrayList;

    //这是一个底部弹出的对话框，用于显示播放列表，用户可以选择播放列表中的视频文件
public class PlaylistDialog extends BottomSheetDialogFragment {
    ArrayList<MediaFiles> arrayList = new ArrayList<>();        //存储视频文件的列表
    VideoFilesAdapter videoFilesAdapter;                        //视频文件适配器
    BottomSheetDialog bottomSheetDialog;                        //底部弹出对话框
    RecyclerView recyclerView;                                  //用于显示视频文件列表的RecyclerView
    TextView folder;

        public PlaylistDialog(ArrayList<MediaFiles> arrayList, VideoFilesAdapter videoFilesAdapter) {
            this.arrayList = arrayList;
            this.videoFilesAdapter = videoFilesAdapter;
        }

    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        bottomSheetDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);           //创建一个底部弹出对话框
        View view = LayoutInflater.from(getContext()).inflate(R.layout.playlist_bs_layout, null);   //加载布局文件
        bottomSheetDialog.setContentView(view);                                                     //设置底部弹出对话框的内容

        recyclerView = view.findViewById(R.id.playlist_rv);                                     //获取RecyclerView
        folder = view.findViewById(R.id.playlist_name);                                         //获取TextView

        SharedPreferences preferences = this.getActivity().getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);
        String folderName = preferences.getString("playlistFOlderName","abc");          //获取播放列表的文件夹名称，如果没有则默认为abc
        folder.setText("播放列表");                                                             //设置TextView的文本为文件夹名称

//        arrayList = fetchMedia(folderName);
        videoFilesAdapter = new VideoFilesAdapter(arrayList, getContext(),1);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(videoFilesAdapter);
        videoFilesAdapter.notifyDataSetChanged();

        return bottomSheetDialog;

    }

    private ArrayList<MediaFiles> fetchMedia(String folderName) {
        ArrayList<MediaFiles> videoFiles = new ArrayList<>();
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

        String selection = MediaStore.Video.Media.DATA+" like?";
        String[] selectionArg = new String[]{"%"+folderName+"%"};
        Cursor cursor = getContext().getContentResolver().query(uri, null,
                selection, selectionArg, null);
        if (cursor != null && cursor.moveToNext()) {
            do {
                String id = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
                String displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
                String size = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.SIZE));
                String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
                String dateAdded = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATE_ADDED));
                MediaFiles mediaFiles = new MediaFiles(id, title, displayName, size, duration, path,
                        dateAdded);
                videoFiles.add(mediaFiles);
            }while (cursor.moveToNext());
        }
        return videoFiles;
    }

}
